package com.ccl.iot.views;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

public abstract class CustomAdapter<TYPE> extends BaseAdapter {
    private List<TYPE> mList;
    private int mLenth;
    public int mWidth;
    private View mLastView;

    private static final int TYPE_NORMAL_VIEW = 0;
    private static final int TYPE_LAST_VIEW = 1;
    public boolean isDialog;

    public CustomAdapter(Context context) {
    }

    @Override
    public int getCount() {
        mList = getDevList();
        if (isDialog){
            mLenth = mList.size();
        }else {
            mLenth = (mList == null ? 1 : (mList.size() + 1));
        }
        return mLenth;
    }

    @Override
    public TYPE getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        switch (getItemViewType(position)) {
            case TYPE_NORMAL_VIEW:
                if (convertView == null) {
                    convertView = getNormalView(position);
                }
                bindNormalView(convertView, position);
                break;
            case TYPE_LAST_VIEW:
                if (mLastView == null) {
                    mLastView = getLastView();
                }
                convertView = mLastView;
                break;
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {

        if (mList == null) {
            return TYPE_LAST_VIEW;
        }else {
            if (isDialog) {
                return TYPE_NORMAL_VIEW;
            } else {
                return position == mList.size() ? TYPE_LAST_VIEW : TYPE_NORMAL_VIEW;
            }
        }

    }

    public abstract List<TYPE> getDevList();

    public abstract View getLastView();

    public abstract View getNormalView(int position);

    public abstract void bindNormalView(View convertView, int position);
}
